<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
</head>
<body>
<div class="nav">
    五子棋对战
</div>
<div class="login-container">
    <div class="login-dialog">
        <h3>注册</h3>
        <div class="row">
            <span>用户名</span>
            <input type="text" id="username">
        </div>
        <div class="row">
            <span>密码</span>
            <input type="password" id="password">
        </div>
        <div class="row">
            <span>确认密码</span>
            <input type="password" id="confirmPassword">
        </div>
        <div class="row">
            <button id="submit">提交</button>
        </div>
    </div>
</div>

<script src="js/jquery.min.js"></script>
<script>
    let usernameInput = document.querySelector('#username');
    let passwordInput = document.querySelector('#password');
    let confirmPasswordInput = document.querySelector('#confirmPassword');
    let submitButton = document.querySelector('#submit');

    submitButton.onclick = function() {
        let username = usernameInput.value.trim();
        let password = passwordInput.value.trim();
        let confirmPassword = confirmPasswordInput.value.trim();

        // 验证表单输入
        if (username === '') {
            alert("请输入用户名");
            usernameInput.value = '';
            passwordInput.value = '';
            confirmPasswordInput.value = '';
            return;
        }
        if (password === '') {
            alert("请输入密码");
            usernameInput.value = '';
            passwordInput.value = '';
            confirmPasswordInput.value = '';
            return;
        }
        if (confirmPassword === '') {
            alert("请输入密码");
            passwordInput.value = '';
            confirmPasswordInput.value = '';
            return;
        }
        if (password.length < 6) {
            alert("密码长度至少为6");
            passwordInput.value = '';
            confirmPasswordInput.value = '';
            return;
        }
        if (!/[A-Z]/.test(password) && !/[a-z]/.test(password) && !/\d/.test(password) && !/[^A-Za-z0-9]/.test(password)) {
            alert("密码强度不够，至少包含两种类型的字符");
            passwordInput.value = '';
            confirmPasswordInput.value = '';
            return;
        }
        if (password !== confirmPassword) {
            alert("两次输入的密码不一致");
            passwordInput.value = '';
            confirmPasswordInput.value = '';
            return;
        }

        submitButton.disabled = true;
        $.ajax({
            type: 'post',
            url: '/register',
            data: {
                username: username,
                password: password,
                confirmPassword: confirmPassword
            },
            success: function(body) {
                if (body && body.username) {
                    alert("注册成功!");
                    location.assign('/login.html');
                } else if (body && body.message) {
                    alert(body.message);
                    if (body.message === "用户名已存在") {
                        usernameInput.value = '';
                    } else {
                        passwordInput.value = '';
                        confirmPasswordInput.value = '';
                    }
                } else {
                    alert("注册失败!");
                }
            },
            error: function() {
                alert("注册失败!");
            },
            complete: function() {
                submitButton.disabled = false;
            }
        });
    }
</script>
</body>
</html>